package com.tengjunjie.qianqianaccountbook.qianqianaccountbook.service.impl;

import com.tengjunjie.qianqianaccountbook.qianqianaccountbook.entity.UserEntity;
import com.tengjunjie.qianqianaccountbook.qianqianaccountbook.enums.ResultEnum;
import com.tengjunjie.qianqianaccountbook.qianqianaccountbook.exception.AccountBookException;
import com.tengjunjie.qianqianaccountbook.qianqianaccountbook.mapper.UserMapper;
import com.tengjunjie.qianqianaccountbook.qianqianaccountbook.service.UserService;
import com.tengjunjie.qianqianaccountbook.qianqianaccountbook.utils.KeyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;

/**
 * Created by tengjunjie on 2017/12/3.
 */
@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public int createUser(UserEntity userEntity) {
        int res=0;
        try{
            userEntity.setId(KeyUtil.genUniqueKey());//生成主键
            userEntity.setCreateTime(new java.util.Date());
            userEntity.setUpdateTime(new java.util.Date());
            userMapper.insert(userEntity);
            res=res+1;
        }catch (Exception ex){
            throw new AccountBookException(ResultEnum.SAVE_ERROR.getCode(),"增加用户信息，插入数据库报错:"+ex.toString());
        }
        return res;
    }
}
